/*
 * Copyright (c) 2022 Institute of Software, Chinese Academy of Sciences (ISCAS)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package xyz.eulix.space;

import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Intent;
import android.os.Build;

import xyz.eulix.space.util.ConstantField;
import xyz.eulix.space.util.Logger;

/**
 * @author: chenjiawei
 * date: 2021/6/18 14:37
 */
public class EulixSpaceJobService extends JobService {
    private static final String TAG = EulixSpaceJobService.class.getSimpleName();

    @Override
    public boolean onStartJob(JobParameters params) {
        Logger.d(TAG, "on start job: " + params.toString());
        Intent intent = new Intent(EulixSpaceJobService.this, EulixSpaceService.class);
        intent.setAction(ConstantField.Action.JOB_ACTION);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && EulixSpaceApplication.getForegroundActivityNumber() <= 0) {
            intent.putExtra(ConstantField.EXTRA.FOREGROUND, true);
            try {
                startForegroundService(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            intent.putExtra(ConstantField.EXTRA.FOREGROUND, false);
            try {
                startService(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        jobFinished(params, false);
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Logger.d(TAG, "on stop job: " + params.toString());
        return true;
    }
}
